/**
 * file created at 2010-6-18 下午09:01:36
 */
package org.lightframework.jamwiki.servlets;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.ehcache.Element;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jamwiki.DataAccessException;
import org.jamwiki.Environment;
import org.jamwiki.WikiException;
import org.jamwiki.WikiMessage;
import org.jamwiki.db.WikiDatabase;
import org.jamwiki.model.Topic;
import org.jamwiki.model.TopicType;
import org.jamwiki.servlets.JAMWikiServlet;
import org.jamwiki.servlets.ServletUtilEx;
import org.jamwiki.servlets.WikiPageInfo;
import org.jamwiki.utils.WikiCache;
import org.jamwiki.utils.WikiUtil;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author fenghm(live.fenghm@gmail.com)
 */
public class SpecialTopicServlet extends JAMWikiServlet {

	private static final String SPECIAL_TOPIC_WIKI  = "special:topic";
	private static final String SPECIAL_TOPIC_CACHE = SpecialTopicServlet.class.getName() + "$" + SPECIAL_TOPIC_WIKI;
	
	@Override
	protected ModelAndView handleJAMWikiRequest(HttpServletRequest request, HttpServletResponse response, ModelAndView next, WikiPageInfo pageInfo)
	        throws Exception {

		view(request,next,pageInfo);
		
		return next;
	}

	private void view(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo) throws Exception {
		//String _topicName = WikiUtil.getTopicFromURI(request);
		String topicName  = request.getParameter("topic");// _topicName.substring("Special:Topic:".length());
		
		String virtualWiki = pageInfo.getVirtualWikiName();
		if (StringUtils.isBlank(virtualWiki)) {
			virtualWiki = Environment.getValue(Environment.PROP_VIRTUAL_WIKI_DEFAULT);
		}
		Topic topic = initializeTopic(virtualWiki, topicName);
//		if (topic.getTopicId() < 0) {
//			// topic does not exist, display empty page
//			WikiMessage wikiMessage = new WikiMessage("topic.notcreated");
//			// topic name is escaped from WikiUtil.getTopicFromURI, so do not double-escape
//			wikiMessage.setParamsWithoutEscaping(new String[]{topicName});
//			next.addObject("notopic", wikiMessage);
//		}
		WikiMessage pageTitle = new WikiMessage("topic.title", topicName);
		ServletUtilEx.viewTopic(request, next, pageInfo, pageTitle, topic, false, true);
	}
	
	private static Topic initializeTopic(String virtualWiki, String topicName) throws WikiException,IOException,DataAccessException {
		WikiUtil.validateTopicName(virtualWiki, topicName);
		
		String key = WikiCache.key(SPECIAL_TOPIC_WIKI, topicName);
		Element cacheElement = WikiCache.retrieveFromCache(SPECIAL_TOPIC_CACHE, key);
		if (cacheElement != null) {
			Topic cacheTopic = (Topic)cacheElement.getObjectValue();
			return cacheTopic;
		}
		
		String pagePath = topicName.replaceAll(":", "/");
		String fileName = "/" + WikiDatabase.SPECIAL_PAGE_DIR + "/" + "Special" + "/" + pagePath + ".txt";
		String content  = read(fileName);
			
		Topic topic = new Topic(SPECIAL_TOPIC_WIKI,topicName);
		topic.setAdminOnly(true);
		topic.setReadOnly(true);
		topic.setTopicType(TopicType.SYSTEM_FILE);
		topic.setCurrentVersionId(0);
		
		if(null != content){
			topic.setTopicContent(content);
		}else{
			topic.setTopicContent("NotExists");
		}
		WikiCache.addToCache(SPECIAL_TOPIC_CACHE, key, topic);
		
		return topic;
	}	
	
	private static String read(String resource) throws IOException{
		InputStream in = SpecialTopicServlet.class.getResourceAsStream(resource);
		if(null != in){
			return IOUtils.toString(in,"UTF-8");
		}else{
			return null;
		}
	}
	
}
